import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;


import javax.imageio.ImageIO;
import javax.swing.JPanel;

import Jama.Matrix;

public class ImageHandler extends JPanel {
	private String fileName;
	private BufferedImage bufferedImage;
	private String extension;
	private String title;
	private Matrix imageMatrix;
	private int height, width;
	
	 public ImageHandler(int width, int height)
	 {
	   bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY);
	   title = "New Picture";
	   extension = "jpg";
	 }
	 
	public ImageHandler(String fileName, int height, int width){
		imageMatrix = new Matrix(height, width);
		this.height = height;
		this.width = width;
		load(fileName);
	}
	
	public ImageHandler(Matrix imageMatrix, int width, int height){
		this.width=width;
		this.height=height;
		this.imageMatrix = imageMatrix;
	}
	
	public void setImageMatrix(Matrix imageMatrix){
		this.imageMatrix = imageMatrix;
	}
	
	public Matrix getImageMatrix(){
		return imageMatrix;
	}
	
	public void getMatrix(File file){
		BufferedImage img;
		try {
			img = ImageIO.read(file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public int getHeight(){
		return height;
	}
	
	public int getWidth(){
		return width;
	}
	
	 public void load(Image image)
	 {
	   // get a graphics context to use to draw on the buffered image
	   Graphics2D graphics2d = bufferedImage.createGraphics();
	   
	   // draw the image on the buffered image starting at 0,0
	   graphics2d.drawImage(image,0,0,null);
	 }
	 
	 public boolean load(String fileName)
	 {
	   boolean result = true; // the default is an okay result
	   
	    // set the current piture's file name
	   this.fileName = fileName;
	   
	   // set the extension
	   int posDot = fileName.indexOf('.');
	   if (posDot >= 0)
	     this.extension = fileName.substring(posDot + 1);
	   
	   // if the current title is null use the file name
	   if (title == null)
	     title = fileName;
	   
	   // try to get the buffered image from the file
	   try {
	     bufferedImage = ImageIO.read(new File(this.fileName));
	     
	   /* if anything goes wrong tell the user on the console, set the
	    * image to a black image with a message on it, and return false */
	   } catch (Exception ex) {
	     System.out.println("Couldn't load the file " + this.fileName);
	     bufferedImage = new BufferedImage(600,200, BufferedImage.TYPE_USHORT_GRAY);
	     result = false;
	   }
	   
	   return result;
	 }
	 
	 public Matrix getPixel(){
		 for(int i = 0; i < height; i++){
			 for(int j = 0; j < width; j++){
				 double x = Math.abs(bufferedImage.getRGB(j,i));
				 x = Math.cbrt(x);
				 imageMatrix.set(i,j,x);
			 }
		 }
		 return imageMatrix;
		 
	 }
	 
	 public void matrixtoString(){
		 for(int i = 0; i < height; i++){
			 for(int j = 0; j < width; j++){
				 System.out.println(imageMatrix.get(i,j));
			 }
		 }
	 }
	 
	 public void paint(Graphics g) {
	        g.drawImage(bufferedImage, 0, 0, null);
	    }
	 
	 public void matrixToImage(int width, int height){
		 bufferedImage = new BufferedImage(width, height, 11);
		 for(int i = 0; i < height; i++){
			 for(int j = 0; j < width; j++){
				 bufferedImage.setRGB(j, i, (int) (Math.pow(imageMatrix.get(i,j),3.0))*-1);
				 //bufferedImage.setRGB(j, i, (int) Math.cbrt((imageMatrix.get(i,j))));
			 }
		 }
	 }

}
